<?php
namespace Application\Admin\Controller;

class File extends Base
{

    public function init()
    {
        $this->setDir('file');
    }

    public function _initRoute()
    {
        $data = [
            'path_new' => '/Admin/File/new',
            'path_edit' => '/Admin/File/edit',
            'path_index' => '/Admin/File/index',
            'path_show' => '/Admin/File/show',
            'path_delete' => '/Admin/File/delete'
        ];
        $this->setCommonData($data);
    }

    private function getService()
    {
        return $this->get(SERVICE_FILE);
    }

    public function indexAction()
    {
        $serviceFile = $this->get(SERVICE_FILE);
        $serviceFileCategory = $this->get(SERVICE_FILECATEGORY);
        $data = [];
        $list1 = $serviceFile->getCategoryCount();
        $map = array2mapObject($list1, 'dry_category');
        $list2 = $serviceFileCategory->getList();
        foreach($list2 as $k => $v){
            $v->total = 0;
            if(isset($map[$v->id])){
                $v->total = $map[$v->id]->total;
            }
            $list2[$k] = $v;
        }
        $data['categoryList'] = $list2;
        $data['domain'] = getOssDomain();
        $this->setFile($this->getTemplateIndex());
        $this->setData($data);
        $this->show();
    }

    public function newAction()
    {
        if($this->isPost()){
            $service = $this->getService();
            $serviceOssFile = $this->get(SERVICE_OSSFILE);
            $post = $this->getPostData();
            $list = $serviceOssFile->more($post['fileId']);
            foreach($list as $v){
                $temp = json_decode(json_encode($v), true);
                $temp['dry_category'] = $post['category'];
                $temp['dry_oss_file'] = $temp['id'];
                unset($temp['id']);
                $service->add($temp);
            }
            $this->sendSuccess();
        }
        else{
            $this->setFile($this->getTemplateNew());
            $this->setData(['action' => 'new']);
            $this->show();
        }
    }

    public function getUploadDataAction()
    {
        $data = $this->get(SERVICE_OSS)->getUploadData();
        $this->sendSuccess($data);
    }

    public function listAction()
    {
        $data = $this->getRequestData();
        $data = [
            'idIn' => $data['idIn']
        ];
        $this->setTemplate('list', 'list');
        $this->setFile($this->getTemplate('list'));
        $this->setData($data);
        $this->show();
    }

}